let {etherscanAK} = require("./config");
let bip39 = require('bip39');
let hdkey = require('ethereumjs-wallet/hdkey');
let util = require('ethereumjs-util');
let Web3 = require('web3');
let QrCode = require('qrcode');
let MnemonicProvider = require("truffle-hdwallet-provider");
let PrivateKeyProvider = require("truffle-hdwallet-provider-privkey");
let Wallet = require('ethereumjs-wallet');


let initProviderFromMnemonic = (mnemonic,infuraAK) => {
  return new MnemonicProvider(mnemonic, infuraAK)
}

let initWeb3FromPrivateKey= async (privateKey) => {

  const provider = new PrivateKeyProvider([privateKey], "http://154.204.31.226:14441/entaike/eth");
  let web3 = new Web3(provider);
  let address = provider.addresses[0];
  let priKey = provider.wallets[address]._privKey.toString('hex');
  let pubKey = provider.wallets[address]._pubKey.toString('hex');
  return {web3, provider, priKey, pubKey, address}
}

let initProviderFromPrivateKey = async (privateKey,infuraAK) => {
  let privKeys = [];
  privKeys.push(privateKey);
  return new PrivateKeyProvider(privKeys, infuraAK);
}

let getMnemonic = () => {
  let mnemonic = bip39.generateMnemonic();
  return mnemonic;
};

let initWeb3FromProvider = async (provider) => {
  let web3 = new Web3(provider);
  let address = provider.addresses[0];
  let priKey = provider.wallets[address]._privKey.toString('hex');
  let pubKey = provider.wallets[address]._pubKey.toString('hex');
  return {web3, provider, priKey, pubKey, address}
}
// let provider=initProviderFromPrivateKey('51c76bdb12fb19538b92bc494e1c9d0c404f71a8e88647f637c588dd348a86b8');
// console.log(initWeb3FromProvider(provider))
//

let initWeb3FromMnemonic = (mnemonic,infuraAK) => {
  if (!mnemonic) {
    mnemonic = getMnemonic();
  }

  let provider = new MnemonicProvider(mnemonic, infuraAK);
  let web3 = new Web3(provider);
  console.log(web3.eth.accounts.create())
  let address = provider.addresses[0];
  let priKey = provider.wallets[address]._privKey.toString('hex');
  let pubKey = provider.wallets[address]._pubKey.toString('hex');
  return {web3, provider, priKey, pubKey, address}
}

let initWalletFromMnemonic = (mnemonic,infuraAK) => {
  if (!mnemonic) {
    mnemonic = getMnemonic();
  }
  let provider = new MnemonicProvider(mnemonic, infuraAK);

  let wallet = {};
  // let privateKey=provider.
  console.log(provider)
}

// console.log(initWalletFromMnemonic())
// console.log(initProviderFromMnemonic(getMnemonic()))
// console.log(initProviderFromPrivateKey('51c76bdb12fb19538b92bc494e1c9d0c404f71a8e88647f637c588dd348a86b8'))
// let provider=initWeb3FromMnemonic('toddler copy giraffe elbow noise afraid midnight wealth exact sketch reject venue');
// console.log(provider)
let getAccountFromMnemonic = (mnemonic, password) => {
  // if (!mnemonic) {
  //   mnemonic = getMnemonic();
  // }
  // console.log(mnemonic)
  // console.log(mnemonic)
  let seed = bip39.mnemonicToSeed(mnemonic);
  let hdWallet = hdkey.fromMasterSeed(seed);

  let key = hdWallet.derivePath("m/44'/60'/0'/0/0");
  let priKey = util.toChecksumAddress(key._hdkey._privateKey.toString('hex'))
  let pubKey = util.toChecksumAddress(key._hdkey._publicKey.toString('hex'))

  //将地址byte arr转换为大写16进制
  let address = util.pubToAddress(key._hdkey._publicKey, true);
  address = util.toChecksumAddress(address.toString('hex'));
  let balance = 0;
 // let pk=priKey.substring(2,priKey.length)
 //  console.log(pk)
 //  let provider=new PrivateKeyProvider([pk], infuraMainAK);
 // console.log(provider)
 //  let web3=new Web3(provider);
  return {mnemonic, seed, priKey, pubKey, address, balance, password,nonce:''}
}

let generateSeedFromPriKey= (priKey,password)=>{
  let wallet = Wallet.fromPrivateKey(priKey);
  return wallet.toV3String(password);
}


let getAccountFromPrivateKey = (privateKey, password) => {
  var fdStart = privateKey.indexOf('0x');
  if (fdStart != -1) {
    privateKey = privateKey.substring(2, privateKey.length);
  }
  var key = Buffer.from(privateKey, 'hex');
  var wallet = Wallet.fromPrivateKey(key);
  let pubKey='0x' +wallet.getPublicKey().toString('hex')
  let address='0x' +wallet.getAddress().toString('hex')
  let balance = 0;
  return {mnemonic:'', seed:'', priKey:privateKey, pubKey, address, balance, password}
}

let getAccountFromKeystore = (keystore, password) => {
  try {
    let wallet=Wallet.fromV3(keystore,password)
    let priKey='0x'+wallet.getPrivateKey().toString('hex')
    let pubKey='0x'+wallet.getPublicKey().toString('hex')
    let address='0x'+wallet.getAddress().toString('hex')
    let balance = 0;
    return {mnemonic:'', seed:'', priKey, pubKey, address, balance, password}
  }catch (e) {
    // throw e
    return -1;
  }
}
// let account=getAccountFromPrivateKey('0x51c76bdb12fb19538b92bc494e1c9d0c404f71a8e88647f637c588dd348a86b8','yang5087633')
// console.log(account)

//生成二维码
let generatePublicKeyQrCode = (domId, msg) => {
  let canvas = document.getElementById(domId);
  QrCode.toCanvas(canvas, msg, function (error) {
    if (error) console.error(error);
  })
};

let generateKeyStore = (privateKey, password) => {
  let key = Buffer.from(privateKey, 'hex');
  let wallet = Wallet.fromPrivateKey(key);
  return wallet.toV3String(password);
}

// export default {
//   initProviderFromMnemonic,
//   initProviderFromPrivateKey,
//   getMnemonic,
//   initWeb3FromProvider,
//   initWeb3FromMnemonic,
//   initWalletFromMnemonic,
//   getAccountFromMnemonic,
//   getAccountFromPrivateKey,
//   generatePublicKeyQrCode,
//   generateKeyStore
// };
let accountUtils = {
  initWeb3FromPrivateKey,
  initProviderFromMnemonic,
  initProviderFromPrivateKey,
  getMnemonic,
  initWeb3FromProvider,
  initWeb3FromMnemonic,
  initWalletFromMnemonic,
  getAccountFromMnemonic,
  getAccountFromPrivateKey,
  getAccountFromKeystore,
  generatePublicKeyQrCode,
  generateKeyStore
}
// module.exports=modul;
// console.log(modul)
// export default accountUtils;

export default accountUtils;
//module.exports=accountUtils;
